<!--  -->
<template>
  <a-card :bordered="false">
    <s-table
      ref="table"
      rowKey="id"
      :columns="$auth('PROJECT.SYSTEM_ACCOUNT_LIST') ? columns : []"
      bordered
      :loading="loading"
      :data="loadData"
      :scroll="{ x: 1500 }"
    >
      <!-- 表头区域 -->
      <template slot="title">
        <a-button
          style="margin-right: 16px"
          type="primary"
          @click="addVisible()"
          v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_ADD')"
        >
          {{ $t('all.autotext1063') }}
        </a-button>
        <a-button
          style="margin-right: 16px"
          type="default"
          icon="download"
          @click="clickExport"
          v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_EXPORT')"
        >
          {{ $t('all.autotext1014') }}
        </a-button>
        <a-button
          style="margin-right: 16px"
          type="dashed"
          icon="download"
          v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_ADD')"
          :href="'/payment/muban/用户导入模板.xlsx' | dealImageUrl"
        >
          {{ $t('all.autotext626') }}
        </a-button>
        <a-upload
          ref="import"
          accept=".xlsx"
          name="file"
          list-type="picture"
          :preview-file="clickImport"
          v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_ADD')"
        >
          <a-button type="default" icon="upload">{{ $t('all.autotext990') }}</a-button>
        </a-upload>
        <!-- 查询条件 -->
        <div class="table-page-search-wrapper" style="margin-top: 20px">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('all.autotext977')">
                  <a-input v-model="queryParam.name" />
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="6" :sm="24">
                  <a-form-model-item :label="$t('all.autotext551')" labelAlign="left">
                    <a-input v-model="queryParam.account" />
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item :label="$t('all.autotext709')" labelAlign="left">
                    <a-input v-model="queryParam.phone" />
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item :label="$t('all.autotext898')" labelAlign="left">
                    <a-select v-model="queryParam.roleId" allowClear>
                      <a-select-option v-for="item in rolesList" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item :label="$t('all.autotext175')" labelAlign="left">
                    <a-select v-model="queryParam.status" allowClear>
                      <a-select-option :value="1">{{ $t('all.autotext307') }}</a-select-option>
                      <a-select-option :value="-1">{{ $t('all.autotext567') }}</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item :label="$t('all.autotext423')" labelAlign="left">
                    <a-select v-model="queryParam.isAdmin" allowClear>
                      <a-select-option :value="1">{{ $t('all.autotext279') }}</a-select-option>
                      <a-select-option :value="0">{{ $t('all.autotext957') }}</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
              </template>
              <a-col :md="(!advanced && 8) || 24" :sm="24">
                <span
                  class="table-page-search-submitButtons"
                  :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
                >
                  <a-button type="primary" @click="$refs.table.refresh(true)">{{ $t('all.autotext30') }}</a-button>
                  <a-button style="margin-left: 8px;" @click="resetQueryParam">{{ $t('all.autotext340') }}</a-button>

                  <a @click="toggleAdvanced" style="margin-left: 8px;">
                    {{ advanced ? $t('all.autotext760.1') : $t('all.autotext760.2') }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </template>
      <!-- 用户类型 -->
      <template slot="type" slot-scope="text, record">
        <span v-if="record.type == 1">{{ $t('all.autotext259') }}</span>
        <span v-if="record.type == 2">{{ $t('all.autotext1175') }}</span>
      </template>
      <!-- 状态 -->
      <template slot="status" slot-scope="text, record">
        <a-tag v-if="record.status == -1" color="#f50">{{ $t('all.autotext567') }}</a-tag>
        <a-tag v-if="record.status == 1" color="#87d068">{{ $t('all.autotext307') }}</a-tag>
      </template>
      <!-- 操作区域 -->
      <template slot="isAdmin" slot-scope="text, record">
        <span v-if="record.isAdmin === 1">{{ $t('all.autotext279') }}</span>
        <span v-else>{{ $t('all.autotext957') }}</span>
      </template>
      <template slot="operation" slot-scope="text, record">
        <a-space>
          <a-button
            v-if="record.status == 1 && $auth('SYSTEM.SYSTEM_ACCOUNT_STATUS')"
            type="warning"
            size="small"
            @click="activeDisable(-1, record.id)"
          >
            {{ record.status !== -1 ? $t('all.autotext567') : $t('all.autotext307') }}
          </a-button>
          <a-button
            v-if="record.status == -1 && $auth('SYSTEM.SYSTEM_ACCOUNT_STATUS')"
            type="warning"
            size="small"
            @click="activeDisable(1, record.id)"
          >
            {{ record.status !== 1 ? $t('all.autotext307') : $t('all.autotext567') }}
          </a-button>
          <!--epv1.5 权限调整-del: $auth('SYSTEM.SYSTEM_ACCOUNT_DETAIL')-->
          <a-button
            type="primary"
            size="small"
            @click="activeEdit(record, false)"
            v-if="!$auth('SYSTEM.SYSTEM_ACCOUNT_EDIT')"
          >
            {{ $t('all.autotext568') }}
          </a-button>
          <a-button
            type="primary"
            size="small"
            @click="activeEdit(record, true)"
            v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_EDIT')"
          >
            {{ $t('all.autotext919') }}
          </a-button>
          <a-popconfirm
            :title="$t('all.autotext313')"
            :ok-text="$t('all.autotext792')"
            :cancel-text="$t('all.autotext975')"
            @confirm="resetPwd(record.id)"
          >
            <a-button v-if="isAdmin" type="danger" size="small" >{{ $t('all.autotext569') }}</a-button>
          </a-popconfirm>
        </a-space>
      </template>
    </s-table>

    <!-- 抽屉add -->
    <AddDrawer ref="addDrawerRef" :visible="drawerVisible" @closeDrawer="closeDrawer" />
    <!-- 编辑抽屉 -->
    <EditDrawer
      ref="editDrawer"
      :is-edit="isEdit"
      :editDrawVisible="editDrawVisible"
      @closeEditDrawer="closeEditDrawer"
    />
  </a-card>
</template>

<script>
import store from '@/store'


import { exportExcel } from '@/utils/util'

import AddDrawer from './components/addDrawer'
import EditDrawer from './components/editDrawer'
import { getUserList, switchStatus, userImport, userExport, getRolesList, resetPwd } from '@/api/system/user'
import { buildPaginationObj } from '@/components/Table'

export default {
  name: '',
  components: {
    AddDrawer,
    EditDrawer
  },
  data () {
    const columns = [
      {
        title: this.$t('all.autotext1113'),
        dataIndex: 'userNo'
      },
      {
        title: this.$t('all.autotext977'),
        dataIndex: 'name'
      },
      {
        title: this.$t('all.autotext551'),
        dataIndex: 'account'
      },
      {
        title: this.$t('all.autotext44'),
        dataIndex: 'email'
      },
      {
        title: this.$t('all.autotext709'),
        dataIndex: 'phone'
      },
      {
        title: this.$t('all.autotext1334'),
        dataIndex: 'position'
      },
      {
        title: this.$t('all.autotext1495'),
        dataIndex: 'roleName'
      },
      // {
      //   title: '电子签名',
      //   dataIndex: 'esignUrl'
      // },
      {
        title: this.$t('all.autotext175'),
        dataIndex: 'status',
        scopedSlots: { customRender: 'status' }
      },
      {
        title: this.$t('all.autotext423'),
        dataIndex: 'isAdmin',
        scopedSlots: { customRender: 'isAdmin' }
      },
      {
        title: this.$t('all.autotext187'),
        dataIndex: 'operation',
        fixed: 'right',
        width: 250,
        scopedSlots: { customRender: 'operation' }
      }
    ]
    return {
      drawerVisible: false,
      editDrawVisible: false,
      columns,
      loading: false,
      queryParam: {
        username: null,
        name: null,
        account: null,
        phone: null,
        roleId: null,
        status: null,
        isAdmin: ''
      },
      rolesList: [],
      advanced: false,
      loadData: parameter => {
        if (this.$auth('PROJECT.SYSTEM_ACCOUNT_LIST')) {
          return getUserList(Object.assign(parameter, this.queryParam)).then(res => {
            return res
          })
        } else {
          return Promise.resolve(buildPaginationObj([]))
        }
      },
      isEdit: false
    }
  },
  created () {
    this.isAdmin = store.getters.userInfo && store.getters.userInfo.id === 1
    this.getRolesList()
  },
  methods: {
    // 获取角色列表
    getRolesList () {
      getRolesList()
        .then(res => {
          this.rolesList = res
        })
    },
    // 点击展开 详细搜索信息
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    // 点击重置
    resetQueryParam () {
      this.queryParam = {
        username: null,
        name: null,
        account: null,
        phone: null,
        roleId: null,
        status: null,
        isAdmin: ''
      }
      this.$refs.table.refresh(true)
    },
    // 新增抽屉
    addVisible () {
      this.drawerVisible = true
    },
    // 编辑当前行
    activeEdit (row, flag) {
      this.isEdit = flag
      const rowObj = JSON.parse(JSON.stringify(row))
      this.$refs.editDrawer.isAdminName = rowObj.roleName
      delete rowObj.createTime
      delete rowObj.roleName
      this.$refs.editDrawer.form = rowObj
      console.log(rowObj, this.$t('all.autotext900'))
      this.$refs.editDrawer.echoDepartment()
      this.$refs.editDrawer.judgeType()
      this.editDrawVisible = true
      this.editUser()
      this.editAccount()
    },
    // 启用禁用操作
    activeDisable (flag, id) {
      switchStatus({
        id,
        status: flag
      }).then(res => {
        if (flag === 1) {
          this.enable()
        } else {
          this.forbidden()
        }
        this.$message.success(this.$t('all.autotext470'))
        this.$refs.table.loadData()
      })
    },
    // 新增对话框关闭 刷新列表
    closeDrawer (val) {
      this.drawerVisible = false
      if (val) {
        this.$refs.table.loadData()
      }
    },
    // 编辑对话框关闭 刷新列表
    closeEditDrawer (val) {
      this.editDrawVisible = false
      if (val) {
        console.log('sss', this.$refs.table)
        this.$refs.table.loadData()
      }
    },
    clickExport () {
      this.exportUser()
      this.exportAccount()
      const exportParams = this.queryParam
      userExport(exportParams).then(res => {
        console.log(res, 'export')
        exportExcel(this.$t('all.autotext1137'), res)
      })
    },
    clickImport (file) {
      const isJpgOrPng = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      if (!isJpgOrPng) {
        this.$notification.error({
          message: file.name,
          description: this.$t('all.autotext1188')
        })
        return false
      } else {
        const params = new FormData()
        params.append('file', file)
        return userImport(params).then(res => {
          this.$notification.success({
            message: file.name,
            description: this.$t('all.autotext946')
          })
          this.importUser()
          this.importAccount()
          console.log(res)
        }).finally(() => {
          this.$refs.import.sFileList = []
          this.$refs.table.loadData()
        })
      }
    },
    // 新增成员和编辑成员组件重新请求获取角色接口
    refreshListData () {
      this.$nextTick(() => {
        this.$refs.addDrawerRef.getInterfaceData()
        this.$refs.editDrawer.getInterfaceData()
      })
    },
    // 导入用户埋点
    importUser () {},
    // 导入账户埋点
    importAccount () {},
    // 导出用户埋点
    exportUser () {},
    // 导出账户埋点
    exportAccount () {},
    // 编辑用户埋点
    editUser () {},
    // 编辑账户埋点
    editAccount () {},
    // 禁用账户
    forbidden () {},
    // 启用账户
    enable () {},
    // 重置密码
    resetPwd (id) {
      resetPwd({ id }).then(res => {
        this.$message.success(this.$t('all.autotext470'))
      })
    }
  }
}
</script>
<style scoped lang="scss"></style>
